package pattern_test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DJView_Servlet extends HttpServlet{
	public void init() throws ServletException{
		BeatModel beatModel = new BeatModel();
		beatModel.initialize();
		getServletContext().setAttribute("beatModel",beatModel);	
	}
	
	// doPost()
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		//doGet()
		BeatModel beatModel = (BeatModel)getServletContext().getAttribute("beatModel");
		
		String bpm = request.getParameter("bpm");
		
		if(bpm == null)
		{
			bpm = beatModel.getBPM() + "";			
		}
		
		String set = request.getParameter("set");
		
		if(set != null)
		{
			int bpmNumber = 90;
			bpmNumber = Integer.parseInt(bpm);
			beatModel.setBPM(bpmNumber);
		}
		
		String decrease = request.getParameter("decrease");
		
		if(decrease != null)
		{
			beatModel.setBPM(beatModel.getBPM() -1);
		}
		
		String increase = request.getParameter("increase");
		
		if(increase != null)
		{
			beatModel.setBPM(beatModel.getBPM() +1);
		}
		
		String on = request.getParameter("on");
		
		if(on != null)
		{
			beatModel.on();
		}
		
		String off = request.getParameter("off");
		
		if(off != null)
		{
			beatModel.off();
		}
		
		request.setAttribute("beatModel", beatModel);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("../../DJView.jsp");
		dispatcher.forward(request, response);
	}

}
